﻿import com.dmotiko.akira.*
import com.dmotiko.general.*

class AkiraApp {
	
	private static var app:AkiraApp;
	public static var HOME:Number = 0;
	public static var BIO:Number = 1;
	public static var WORK:Number = 2;
	public static var CONTACT:Number = 3;
	public static var SECTION:Number;
	public static var ENGLISH:String = "english";
	public static var SPANISH:String = "spanish";
		
	public static function init(){
		if(!app) app = new AkiraApp();
	}
	
	public static function getApp():AkiraApp {
		return app;
	}
	
	private var clipLoader:MovieClipLoader;
	private var stageListener:Object;
	private var logoWatcher:MovieClip;
	private var actualSection:MovieClip;
	
	private var portfolioLoader:XML;
	private var aWorks:Array;
	private var workSelected:Object;
	private var photoSelected:Object;
	
	private var sLanguage:String;
	
	var listeners:Array;
	
	private function AkiraApp(){
		Stage.align = "TL";
		Stage.scaleMode = "noScale";
		clipLoader = new MovieClipLoader();
		clipLoader.addListener();
								
		// create container and childs
		_root.CONTENT_GUIDE = _root.mcContentGuide; //for centering pourposes
		_root.CONTAINER = _root.createEmptyMovieClip("mcContainer", 1);
		_root.CONTAINER.LOGO = _root.CONTAINER.createEmptyMovieClip("mcLogo", _root.CONTAINER.getNextHighestDepth());
		_root.CONTAINER.NAVBAR = _root.CONTAINER.createEmptyMovieClip("mcNavBar", _root.CONTAINER.getNextHighestDepth());
		_root.CONTAINER.NAVBAR._x = 547;
		_root.CONTAINER.MAINPHOTO = _root.CONTAINER.createEmptyMovieClip("mcMainPhoto", _root.CONTAINER.getNextHighestDepth());
		_root.CONTAINER.MAINPHOTO._x = 110;
		_root.CONTAINER.MAINPHOTO._y = 130;
		
		_root.CONTAINER.BIO = _root.CONTAINER.createEmptyMovieClip("mcBio", _root.CONTAINER.getNextHighestDepth());
		_root.CONTAINER.CONTACT = _root.CONTAINER.createEmptyMovieClip("mcContact", _root.CONTAINER.getNextHighestDepth());
		_root.CONTAINER.PORTFOLIO = _root.CONTAINER.createEmptyMovieClip("mcPortfolio", _root.CONTAINER.getNextHighestDepth());
		_root.CONTAINER.PORTFOLIO._x = 110;
		_root.CONTAINER.PORTFOLIO._y = 180;
		_root.CONTAINER.PORTFOLIOLEFT = _root.CONTAINER.createEmptyMovieClip("mcPortfolioLeft", _root.CONTAINER.getNextHighestDepth());
		_root.CONTAINER.PORTFOLIOLEFT._x = 10;
		_root.CONTAINER.PORTFOLIOLEFT._y = 200;
				
		// load portfolio
		portfolioLoader = new XML();
		this.loadPortfolio();
				
		initStageListener();
	}
	
	public function loadPortfolio(){
		portfolioLoader.ignoreWhite = true;
		portfolioLoader.load("portfolio.xml");
		portfolioLoader.onLoad = function(){
			var _aworks:Array = new Array();
			var rootNode:XMLNode = this.firstChild;
			var aPhotos:Array;
			for(var i:Number=0;i < rootNode.childNodes.length; i++){
				var workData:XMLNode = rootNode.childNodes[i];
				var work:Object = new Object();
				work.ID = workData.attributes.id;
				work.name = workData.attributes.name;
				aPhotos = new Array();
				var photo:Object;
				for(var i2:Number=0; i2 < workData.childNodes.length; i2++){
					var photoData:XMLNode = workData.childNodes[i2];
					photo = new Object();
					photo.ID = photoData.attributes.id;
					photo.name = photoData.attributes.name;
					photo.description = photoData.attributes.description;
					photo.photo = photoData.attributes.src;
					photo.thumb = photoData.attributes.thumb;
					photo.work = work.ID;
					aPhotos.push(photo);
				}
				work.photos = aPhotos;
				_aworks.push(work);
			}
			AkiraApp.getApp().setWorks(_aworks);
			AkiraApp.getApp().loadSections();
			PhotoPreloader.run();
		}
	}
	
	public function loadSections(){
		// load logo
		clipLoader.loadClip("akiraLogo.swf", _root.CONTAINER.LOGO);
		
		// load navBar
		clipLoader.loadClip("akiraNavBar.swf", _root.CONTAINER.NAVBAR);
		
		// load mainWindow
		clipLoader.loadClip("mainPhoto.swf", _root.CONTAINER.MAINPHOTO);
		
		// load polrtfolio
		clipLoader.loadClip("portfolio.swf", _root.CONTAINER.PORTFOLIO);
		
		// load left polrtfolio
		clipLoader.loadClip("portfolioLeft.swf", _root.CONTAINER.PORTFOLIOLEFT);
		
		// load bio
		clipLoader.loadClip("bio.swf", _root.CONTAINER.BIO);
		
		// load contact
		clipLoader.loadClip("contacto.swf", _root.CONTAINER.CONTACT);
	}
	
	public function onLoadProgress(target:MovieClip, loaded:Number, total:Number):Void {
		
	};
	public function onLoadInit(target:MovieClip):Void {
		
	};
	
	public function childLoaded(movie:MovieClip):Void {
		if(movie == _root.CONTAINER.NAVBAR){
			stageListener.onResize(); 
			_root.CONTAINER.LOGO.mcContent.play();
			logoWatcher = _root.createEmptyMovieClip("_logoWatcher", _root.getNextHighestDepth());
			logoWatcher.onEnterFrame = function(){
				if(_root.CONTAINER.LOGO.mcContent._currentframe == _root.CONTAINER.LOGO.mcContent._totalframes - 5){
					delete this.onEnterFrame;
					_root.CONTAINER.NAVBAR.mcContent.play();
					_root.CONTAINER.MAINPHOTO.show("homeTest.jpg");
					AkiraApp.getApp().actualSection = _root.CONTAINER.MAINPHOTO;
					AkiraApp.SECTION = AkiraApp.HOME;
					removeMovieClip(this);
				}
			}
			
		}
	};
	public function childChanged(movie:MovieClip, data:Object):Void {
		
		if(movie == _root.CONTAINER.LOGO){
			_root.CONTAINER.MAINPHOTO._y = 130;
			_root.CONTAINER.MAINPHOTO.show("homeTest.jpg");
			_root.CONTAINER.NAVBAR.resetNavBar();
			AkiraApp.SECTION = AkiraApp.HOME;
			if(actualSection != _root.CONTAINER.MAINPHOTO) actualSection.hide();
			actualSection = _root.CONTAINER.MAINPHOTO;
			_root.CONTAINER.PORTFOLIOLEFT.hide();
			app.propagateEvent(new AkiraAppEvent(AkiraAppEvent.CHANGE_SECTION, app), AkiraApp.SECTION);
		} 
	};
	
	public function showSection(section:Number):Void {
				
		AkiraApp.getApp().actualSection.hide(hideListener);
		_root.CONTAINER.PORTFOLIOLEFT.hide();
		var app:AkiraApp = AkiraApp.getApp();
		function hideListener(){
			
			switch(section){
				case AkiraApp.HOME:
				AkiraApp.SECTION = AkiraApp.HOME;
				AkiraApp.getApp().actualSection = _root.CONTAINER.MAINPHOTO;
				_root.CONTAINER.MAINPHOTO.show("homeTest.jpg");	
				if(_root.analytics)	getURL(_root.analytics+"('akiraHOME')");
				break;
				case AkiraApp.BIO:
				AkiraApp.SECTION = AkiraApp.BIO;
				AkiraApp.getApp().actualSection = _root.CONTAINER.BIO;
				_root.CONTAINER.BIO.show();
				if(_root.analytics)	getURL(_root.analytics+"('akiraBIO')");
				break;
				case AkiraApp.WORK:
				AkiraApp.SECTION = AkiraApp.WORK;
				AkiraApp.getApp().actualSection = _root.CONTAINER.PORTFOLIO;
				_root.CONTAINER.PORTFOLIO.show();
				if(_root.analytics)	getURL(_root.analytics+"('akiraPORTFOLIO')");
				break;
				case AkiraApp.CONTACT:
				AkiraApp.SECTION = AkiraApp.CONTACT;
				AkiraApp.getApp().actualSection = _root.CONTAINER.CONTACT;
				_root.CONTAINER.CONTACT.show();
				if(_root.analytics)	getURL(_root.analytics+"('akiraCONTACT')");
				break;
			}
			
			app.propagateEvent(new AkiraAppEvent(AkiraAppEvent.CHANGE_SECTION, app), section);
			
		}
	}
	
	public function showPhoto(data):Void {
		
		if(AkiraApp.getApp().actualSection == _root.CONTAINER.MAINPHOTO 
		&&  AkiraApp.SECTION == AkiraApp.WORK){
			hideListener();
		} else {
			AkiraApp.getApp().actualSection.hide(hideListener);
		}
				
		function hideListener(){
			
			 AkiraApp.getApp().photoSelected = data;
			_root.CONTAINER.MAINPHOTO.show(data.photo);
			for(var i:Number=0; i < AkiraApp.getApp().aWorks.length; i++){
				var work = AkiraApp.getApp().aWorks[i];
				if(work.ID == data.work) AkiraApp.getApp().workSelected = work;
			}
			_root.CONTAINER.PORTFOLIOLEFT.show();
			AkiraApp.getApp().actualSection = _root.CONTAINER.MAINPHOTO;
		}
	}
	
	public function setWorks(aWorks:Array):Void {
		this.aWorks = aWorks;
	}
	public function getWorks():Array {
		return this.aWorks;
	}
	public function getWorkSelected():Object {
		return this.workSelected;
	}
	public function setPrevPhotoSelected(evnt, data){
		AkiraApp.getApp().propagateEvent(new AkiraAppEvent(AkiraAppEvent.PREV_PHOTO, AkiraApp.getApp()), photoSelected);
	}
	public function setNextPhotoSelected(evnt, data){
		AkiraApp.getApp().propagateEvent(new AkiraAppEvent(AkiraAppEvent.NEXT_PHOTO, AkiraApp.getApp()), photoSelected);
	}
	public function getPhotoSelected():Object {
		return this.photoSelected;
	}
	
	public function getLanguage():String {
		return this.sLanguage;
	}
	public function setLanguage(sLanguage):Void {
		if(sLanguage != AkiraApp.ENGLISH && sLanguage != AkiraApp.SPANISH) sLanguage = AkiraApp.SPANISH;
		if(sLanguage == this.sLanguage) return;
		this.sLanguage = sLanguage;
		AkiraApp.getApp().propagateEvent(new AkiraAppEvent(AkiraAppEvent.CHANGE_LANGUAGE, AkiraApp.getApp()), sLanguage);
		if(_root.analytics)	getURL(_root.analytics+"('akiraLANGUAGE_"+sLanguage+"')");
	}
	
	public function setNavBarEnabled(bEnabled):Void {
		_root.CONTAINER.NAVBAR.useHandCursor = bEnabled;
		if(!bEnabled){
			_root.CONTAINER.NAVBAR.onPress = function(){ return; }
		} else {
			delete _root.CONTAINER.NAVBAR.onPress;
		}
	}
		
	public function addListener(f:Function, evnt):Void {
		if(!listeners) listeners = new Array();
		listeners.push([f, evnt]);
	}
	public function removeListener(f:Function):Void {
		for(var i:Number = 0; i < listeners.length; i++){
			if(listeners[i][0] == f) listeners.splice(i, 1);
		}
	}
	public function propagateEvent(evnt:BaseEvent, data:Object):Void {
		var oData:Object = new Object();
		for(var i:Number = 0; i < listeners.length; i++){
			
			if(listeners[i][1].getType() == evnt.getType() || !listeners[i][1].getType()){
				oData.bind = listeners[i][1].getTarget();
				oData.data = data;
				listeners[i][0](evnt, oData);
			}
		}
	}
	
	private function initStageListener():Void {
		stageListener = new Object();
		stageListener.onResize = function() {
					
			if(Stage.width > _root.CONTENT_GUIDE._width){
				_root.CONTENT_GUIDE._x = (Stage.width - _root.CONTENT_GUIDE._width) /2;
				_root.CONTENT_GUIDE._y = 0;
			} else {
				_root.CONTENT_GUIDE._x = 0;
				_root.CONTENT_GUIDE._y = 0;
			}
			_root.CONTAINER._x = _root.CONTENT_GUIDE._x;
			_root.CONTAINER._y = _root.CONTENT_GUIDE._y;
			
		};
		stageListener.onResize();
		Stage.addListener(stageListener);
		
	}
	
}